package com.yy.micro.magic.core.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.yy.micro.magic.core.config.Content;

import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;
 /**
   * @author YY
   * @version 1.0.0
   * @update 2021/9/22 9:35 下午
   * @description: 数据变换Rule描述，用于说明数据A是如何变换为B。
 */
public class RuleDescriptor {

    /**
     * 节点在数据库中的位置
     */
    private RuleKey key;

    /**
     * 当前节点 name
     */
    private String name;

    /**
     * 别名
     */
    private String alias;

    /**
     * 原数据集中节点key的值
     */
    private Object value;

    /**
     * 变换策略
     */
    private String strategy;

    /**
     * 计算公式
     */
    private String formula;

    /**
     * 计算时参数
     */
    private List<String> paras;

    /**
     * 小数位
     */
    private int scala = -1;

    /**
     * 输入，用于拷贝策略
     */
    private String input;

    private  String output;

    private boolean syncParent = Boolean.FALSE;

    private  boolean dynamic= Boolean.FALSE;

    /**
     * 格式化
     */
    private String format;

    private String fromFormat;

    /**
     * 扩展属性
     */
    private Map<String, String> properties;

    @JsonIgnore
    private Map<String, Object> configurations;

    public String getKey() {
        return key.getKey();
    }

    public RuleKey getRuleKey(){
        return key;
    }

    public void setKey(final String key) {
        this.key = RuleKey.create(key);
    }

    public String getName() {
        if (StringUtils.isEmpty(name)){
            name = key.getName();
        }
        return name;
    }

    public void setName(final String name) {
        this.name = name;
    }

    public String getAlias() {
        return alias;
    }

    public void setAlias(final String alias) {
        this.alias = alias;
    }

    public Object getValue() {
        return value;
    }

    public void setValue(final Object value) {
        this.value = value;
    }

    public String getStrategy() {
        return strategy;
    }

    public void setStrategy(final String strategy) {
        this.strategy = strategy;
    }

    public String getFormula() {
        return formula;
    }

    public void setFormula(final String formula) {
        this.formula = formula;
    }

    public List<String> getParas() {
        return paras;
    }

    public void setParas(final List<String> paras) {
        this.paras = paras;
    }

    public int getScala() {
        return scala;
    }

    public void setScala(final int scala) {
        this.scala = scala;
    }

    public String getInput() {
        return input;
    }

    public void setInput(final String input) {
        this.input = input;
    }

    public String getFormat() {
        return format;
    }

    public void setFormat(final String format) {
        this.format = format;
    }

    public void setFromFormat(final String fromFormat) {
        this.fromFormat = fromFormat;
    }

    public String getFromFormat() {
        return fromFormat;
    }

    public Map<String, String> getProperties() {
        return properties;
    }

    public void setProperties(final Map<String, String> properties) {
        this.properties = properties;
    }

    public String getOutput() {
        return output;
    }

    public void setOutput(final String output) {
        this.output = output;
    }

    public boolean isDynamic() {
        return dynamic;
    }

    public void setDynamic(final boolean dynamic) {
        this.dynamic = dynamic;
    }

    public void setSyncParent(final boolean syncParent) {
        this.syncParent = syncParent;
    }

    public boolean isSyncParent() {
        return syncParent;
    }

    public Map<String, Object> getConfigurations() {
        return configurations;
    }

    public void setConfigurations(final Map<String, Object> configurations) {
        this.configurations = configurations;
    }

    @JsonIgnore
    public RuleDescriptor copy(){
        RuleDescriptor m = new RuleDescriptor();
        m.alias = this.alias;
        m.scala = this.scala;
        m.format = this.format;
        m.formula = this.formula;
        m.input = this.input;
        m.key = this.key;
        m.name = this.name;
        m.strategy = this.strategy;
        m.value = this.value;
        m.configurations = this.configurations;
        m.paras = this.paras;
        m.properties = this.properties;
        m.dynamic = this.dynamic;
        m.syncParent = this.syncParent;
        m.output = this.output;
        return m;
    }

    public static RuleDescriptor create(String key){
        return create(key, Content.COPY_STRATEGY);
    }

    public static RuleDescriptor create(String key,String strategy){
        RuleDescriptor m = new RuleDescriptor();
        m.setKey(key);
        m.setInput(key);
        m.strategy = strategy;
        return m;
    }
}
